<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-row class="search-input">
          <el-form-item prop="entryType" label="条款类型：">
            <el-select v-model="filters.entryType" placeholder="条款类型">
              <el-option label="商家入驻条款" value="1"></el-option>
              <el-option label="店铺入驻条款" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="status" label="状态：">
            <el-select v-model="filters.status" placeholder="状态">
              <el-option label="停用" value="0"></el-option>
              <el-option label="启用" value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row class="search-button">
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-plus"
              :label="$t('action.add')"
              type="primary"
              @click="handleAdd"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-refresh"
              :label="$t('action.reset')"
              type="primary"
              @click="resetForm('filters')"
            />
          </el-form-item>
          <el-form-item>
            <kt-button label="启用/停用" type="primary" @click="startTerms"/>
          </el-form-item>
        </el-row>
        <el-form-item class="t_r w100">
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
        <!--表格显示列界面-->
        <table-column-filter-dialog
          ref="tableColumnFilterDialog"
          :columns="columns"
          @handleFilterColumns="handleFilterColumns"
        ></table-column-filter-dialog>
      </el-form>
    </div>
    <!--表格内容栏-->
    <jic-table
      :height="350"
      :tableList="pageResult"
      @selectionChange="selectionChange"
      :tableHeaderList="filterColumns"
      @findPage="findPage"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
      @handlePageChange="handlePageChange"
      @handleDetail="handleDetail"
      :totalSize="totalSize"
      :showBatchDelete="false"
      :showEditOperation="true"
      :showDeleteOperation="true"
      :showDetailOperation="true"
      ref="JicTable"
    ></jic-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'编辑'"
      width="50%"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="100px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
      >
        <el-row class="searchDialog-input">
          <el-form-item prop="entryType" label="条款类型：">
            <el-select
              v-model="dataForm.entryType"
              style="width:100%"
              placeholder="条款类型"
              :disabled="typeOpera"
            >
              <el-option
                v-for="item in entryTypeList"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="条款编码：" class="styleReplace2" prop="entryCode">
            <el-input
              v-model="dataForm.entryCode"
              auto-complete="off"
              :disabled="false"
              placeholder="条款编码"
            ></el-input>
          </el-form-item>
          <el-form-item label="条款标题：" class="styleReplace1" prop="title">
            <el-input
              v-model="dataForm.title"
              auto-complete="off"
              :disabled="false"
              placeholder="请输入条款标题"
            ></el-input>
          </el-form-item>
          <el-form-item label="条款状态：" class="styleReplace1" prop="status" v-show="statusShow">
            <el-radio-group v-model="radio" @change="radioChange">
              <el-radio
                v-for="item in status"
                :key="item.value"
                :value="item.value"
                :label="item.value"
              >{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="条款内容：" class="styleReplace3" prop="content">
            <quill-editor
              v-model="dataForm.content"
              ref="myQuillEditor"
              style="height: 300px;"
              :options="editorOption"
            ></quill-editor>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center;margin-top:50px">
        <el-button :size="size" @click.native="dialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          :style="{ display: submitShow }"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import KtButton from "@/views/Core/KtButton";
import { format } from "@/utils/datetime";
import { quillEditor } from "vue-quill-editor";
import JicTable from "@/components/jicComponentGallery/jicTable";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";

export default {
  components: {
    JicTable,
    KtButton,
    TableColumnFilterDialog,
    quillEditor
  },
  data() {
    return {
      //分页
      totalSize: 0,
      statusShow: true,

      //条款类型不可编辑
      typeOpera: false,
      //提交按键隐藏
      submitShow: '',

      status: [
        { label: "启用", value: 1 },
        { label: "停用", value: 0, checked: true }
      ],
      content: null,
      editorOption: {},
      size: "small",
      radio: "",
      filters: {
        entryType: "",
        status: ""
      },
      entryTypeList: [
        { key: 1, value: "商家入驻条款" },
        { key: 2, value: "店铺入驻条款" },
        { key: 3, value: "供应商入驻条款" }
      ],
      columns: [],
      filterColumns: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: [],

      operation: false, // true:新增, false:编辑
      dialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      dataFormRules: {
        entryType: [
          { required: true, message: "请选择条款类型", trigger: "blur" }
        ],
        entryCode: [
          { required: true, message: "请输入条款编码", trigger: "blur" },
          {pattern:"^[0-9a-zA-Z_]{1,}$", message: '仅可输入数字、26个英文字母或者下划线', trigger: 'blur'},
        ],

        content: [
          { required: true, message: "请输入条款内容", trigger: "blur" }
        ]
      },
      // 新增编辑界面数据
      dataForm: {
        entryType: "",
        entryCode: "",
        status: "",
        content: ""
      },
      shopList: [],
      statusList: [],
      selections: []
    };
  },
  methods: {
    // 获取分页数据
    findPage: function(data) {
      if (data != null && data.start != null) {
        if (data.start != null) {
          this.pageRequest.pageNum = data.start;
        }
        if (data.limit != null) {
          this.pageRequest.pageSize = data.limit;
        }
      }

      let this_ = this;
      if (this_.filters == undefined || this_.filters == null) {
        this_.filters = {};
      }
      this_.filters.start = this.pageRequest.pageNum;
      this_.filters.limit = this.pageRequest.pageSize;
      this_.utils.request.queryEntryTermsList(this_.filters, function(res) {
        this_.pageResult = res.data.rows;
        this_.totalSize = Number(res.data.total);
      });
    },

    //左右分页点击
    handlePageChange(params) {
      if (params != null && params.number != null) {
        if (params.number != null) {
          this.pageRequest.pageNum = params.number;
        }

        this.findPage();
      }
    },
    // 详情页面
    handleDetail(params) {
      if (this.$refs["dataForm"]) {
        this.$refs["dataForm"].clearValidate();
      }
      if (params.row.id != null) {
        this.statusShow = false;
      }
      this.dialogVisible = true;
      this.operation = false;
      this.submitShow = 'none';
      this.dataForm = {
        id: params.row.id,
        entryType: params.row.entryType,
        entryCode: params.row.entryCode,
        title: params.row.title,

        content: params.row.content
      };
      this.radio = params.row.status;
    },
    // 显示新增界面
    handleAdd: function() {
      this.submitShow = '';
      this.typeOpera = false;
      this.dialogVisible = true;
      this.operation = true;
      this.radio = 0;
      this.dataForm = {
        id: "",
        entryType: "",
        entryCode: "",
        status: "0",
        content: ""
      };
      this.resetForm("dataForm");
    },
    //选择框
    selectionChange: function(params) {
      this.selections = params.selections;
      console.log(this.selections);
    },
    // 删除
    handleDelete: function(data) {
      this.$confirm("确认删除吗？", "提示", {}).then(() => {
        let ids = data.row.id;
        var params = {};
        params.ids = ids;

        var this_ = this;
        this.utils.request.deleteEntryTerms(params, function(res) {
          if (res.code == "0000") {
            this_.$message({ message: "操作成功", type: "success" });
            this_.findPage(null);
          } else {
            this_.$message({
              message: "操作失败, " + res.msg,
              type: "error"
            });
          }
        });
      });
    },
    // 显示编辑页面
    handleEdit: function(params) {
      if (this.$refs["dataForm"]) {
        this.$refs["dataForm"].clearValidate();
      }
      if (params.row.id != null) {
        this.statusShow = false;
      }
      this.dialogVisible = true;
      this.typeOpera = true;
      this.operation = false;
      this.submitShow = '';
      this.dataForm = {
        id: params.row.id,
        entryType: params.row.entryType,
        entryCode: params.row.entryCode,
        title: params.row.title,

        content: params.row.content
      };
      this.radio = params.row.status;
    },
    // 编辑
    submitForm: function() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            let params = Object.assign({}, this.dataForm);
            params.status = this.radio;
            var this_ = this;
            this.utils.request.editEntryTermsInfo(params, function(res) {
              if (res.code == "0000") {
                this_.$message({ message: "操作成功", type: "success" });

                this_.$refs["dataForm"].resetFields();

                this_.dialogVisible = false;

                this_.findPage(null);
              } else {
                this_.$message({
                  message: "操作失败, " + res.msg,
                  type: "error"
                });
              }
            });
          });
        }
      });
    },

    //radio框选择
    radioChange: function(e) {
      this.dataForm.status = e + "";
      console.log(this.dataForm.status);
    },

    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },

    resetForm(formName) {
      if (this.$refs[formName] != undefined) {
        this.$refs[formName].resetFields();
        this.filters.start_time = "";
        this.filters.end_time = "";
        this.findPage(null);
      }
    },

    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      this.columns = [
        { prop: "entryCode", label: "条款编号", minWidth: 50 },
        { prop: "title", label: "条款标题", minWidth: 120 },
        {
          prop: "entryType",
          label: "条款类型",
          minWidth: 120,
          formatter: function(row, column, cellValue, index) {
            if (cellValue == 1) {
              return "商家入驻条款";
            } else if (cellValue == 2) {
              return "店铺入驻条款";
            } else if (cellValue == 3) {
              return "供应商入驻条款";
            }
          }
        },
        { prop: "deptName", label: "部门名称"},
        { prop: "createTime", label: "创建时间", minWidth: 120 },
        {
          prop: "status",
          label: "启用状态",
          minWidth: 120,
          formatter: function(row, column, cellValue, index) {
            if (cellValue == 0) {
              return "停用";
            } else if (cellValue == 1) {
              return "启用";
            }
          }
        }
      ];
      var temp = [];

      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },

    //启用条款
    startTerms() {
      if (this.selections.length != 1) {
        this.$message({
          message: "请选择一条数据进行处理, ",
          type: "error"
        });
      } else {
        let params = {};
        params.id = this.selections[0].id;
        if(this.selections[0].status=='1'){
          params.status = '0';
        }else if(this.selections[0].status=='0'){
          params.status = '1';
        }
        var this_ = this;
        this.utils.request.startTerms(params, function(res) {
          if (res.code == "0000") {
            this_.$message({ message: "操作成功", type: "success" });
            this_.findPage(null);
          } else {
            this_.$message({ message: "操作失败, " + res.msg, type: "error" });
          }
        });
      }
    }
  },
  mounted() {
    this.initColumns();
    this.findPage();
  }
};
</script>

<style lang="scss" scoped>
</style>

